package com.maycari.captainbi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.maycari.captainbi.common.constant.CaptainBIConstant;
import com.maycari.captainbi.data.dto.Captain47Dto;
import com.maycari.captainbi.data.dto.Captain49Dto;
import com.maycari.captainbi.data.po.Captain46Po;
import com.maycari.captainbi.data.po.Captain47Po;
import com.maycari.captainbi.data.po.Captain49Po;
import com.maycari.captainbi.listener.SyncDto;
import com.maycari.captainbi.listener.SyncListener;
import com.maycari.captainbi.mapper.Captain47Dao;
import com.maycari.captainbi.service.Captain47Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maycari.captainbi.util.CaptainBIUtil;
import com.maycari.tool.utils.BeanUtil;
import com.maycari.tool.utils.CollectionUtil;
import com.maycari.tool.utils.DateUtil;
import com.maycari.tool.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王宏伟
 * @since 2021-04-01
 */
@Service
@Slf4j
public class Captain47ServiceImpl extends ServiceImpl<Captain47Dao, Captain47Po> implements Captain47Service {
    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public List<Captain47Dto> queryListAll() {
        Date start_time = DateUtil.parse("2021-04-01 00:00:00", DateUtil.PATTERN_DATETIME);
        String result = new String();
        try {
            Date end_time = DateUtil.plusMonths(start_time, 1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            int page = 1;
            int page_size = 20;
            String buhuo = "3";
            TreeMap<String, String> params = new TreeMap<>();
            params.put("buhuo",buhuo);
            params.put("page", String.valueOf(page));
            params.put("page_size", String.valueOf(page_size));
            params.put("start_time", simpleDateFormat.format(start_time));
            params.put("end_time", simpleDateFormat.format(end_time));
            String action = "http://cronapi.captainbi.com/index.php?m=cronapi&c=fba&a=getFbaReplenishmentList";

            result = CaptainBIUtil.sendRequest(action, params);

            JSONObject jsonObject = JSON.parseObject(result);
            String data = jsonObject.getString("data");
            System.out.println(data);
            if (StringUtil.isNotBlank(data)) {
                Map<String, Object> map = new HashMap<>();
                List<Captain47Po> captain47Pos = JSON.parseArray(data, Captain47Po.class);
                for (int i = 0; i < captain47Pos.size(); i++) {
                    Captain47Po captain47Po = captain47Pos.get(i);
                        /*String channel_name = captain47Po.getChannelName();
                        String thumb_path = captain47Po.getThumbPath();
                        String available_days = captain47Po.getAvailableDays();
                        String inbound_working_quantity = captain47Po.getInboundWorkingQuantity();
                        String fulfillable_quantity = captain47Po.getFulfillableQuantity();
                        String inbound_receiving_quantity = captain47Po.getInboundReceivingQuantity();
                        String titleS = captain47Po.getTitles();
                        String unsellable_quantity = captain47Po.getUnsellableQuantity();
                        String inbound_shipped_quantity = captain47Po.getInboundShippedQuantity();
                        String modified_time = captain47Po.getModifiedTime();
                        Integer mfn_listing_exist = captain47Po.getMfnListingExist();
                        Integer buhuo = captain47Po.getBuhuo();
                        String reserved_quantity = captain47Po.getReservedQuantity();
                        String asinS = captain47Po.getAsins();
                        String seller_sku = captain47Po.getSellerSku();
                        String suggested_replenishment_time = captain47Po.getSuggestedReplenishmentTime();
                        String available_stock = captain47Po.getAvailableStock();
                        Integer replenishment_quantity = captain47Po.getReplenishmentQuantity();*//*

                        map.put("titles", captain47Po.getTitles());
                        map.put("thumbPath", captain47Po.getThumbPath());
                        map.put("asins", captain47Po.getAsins());
                        map.put("sellerSku", captain47Po.getSellerSku());
                        map.put("mfnListingExist", captain47Po.getMfnListingExist());
                        map.put("channelName", captain47Po.getChannelName());
                        map.put("buhuo", captain47Po.getBuhuo());
                        map.put("availableDays", captain47Po.getAvailableDays());
                        map.put("replenishmentQuantity", captain47Po.getReplenishmentQuantity());
                        map.put("suggestedReplenishmentTime", captain47Po.getSuggestedReplenishmentTime());
                        map.put("fulfillableQuantity", captain47Po.getFulfillableQuantity());
                        map.put("inboundShippedQuantity", captain47Po.getInboundShippedQuantity());
                        map.put("inboundReceivingQuantity", captain47Po.getInboundReceivingQuantity());
                        map.put("availableStock", captain47Po.getAvailableStock());
                        map.put("reservedQuantity", captain47Po.getReservedQuantity());
                        map.put("unsellableQuantity", captain47Po.getUnsellableQuantity());
                        map.put("inboundWorkingQuantity", captain47Po.getInboundWorkingQuantity());
                        map.put("modifiedTime", captain47Po.getModifiedTime());*/

                    save(captain47Po);
                }
            }



                /*JSONObject jsonObject1 = (JSONObject) jsonObject.get("data");
                if (ObjectUtils.isNotEmpty(jsonObject1)) {
                    for (int i = 0; i < jsonObject1.size(); i++) {
                        String channel_name = jsonObject1.getString("channel_name");
                        String thumb_path = jsonObject1.getString("thumb_path");
                        String available_days = jsonObject1.getString("available_days");
                        String inbound_working_quantity = jsonObject1.getString("inbound_working_quantity");
                        String fulfillable_quantity = jsonObject1.getString("fulfillable_quantity");
                        String inbound_receiving_quantity = jsonObject1.getString("inbound_receiving_quantity");
                        String titleS = jsonObject1.getString("TitleS");
                        String unsellable_quantity = jsonObject1.getString("unsellable_quantity");
                        String inbound_shipped_quantity = jsonObject1.getString("inbound_shipped_quantity");
                        String modified_time = jsonObject1.getString("modified_time");
                        String mfn_listing_exist = jsonObject1.getString("mfn_listing_exist");
                        String buhuo = jsonObject1.getString("buhuo");
                        String reserved_quantity = jsonObject1.getString("reserved_quantity");
                        String asinS = jsonObject1.getString("AsinS");
                        String seller_sku = jsonObject1.getString("seller_sku");
                        String suggested_replenishment_time = jsonObject1.getString("suggested_replenishment_time");
                        String available_stock = jsonObject1.getString("available_stock");
                        String replenishment_quantity = jsonObject1.getString("replenishment_quantity");

                        Map<String, Object> map = new HashMap<>();
                        map.put("titles", titleS);
                        map.put("thumbPath", thumb_path);
                        map.put("asins", asinS);
                        map.put("sellerSku", seller_sku);
                        map.put("mfnListingExist", mfn_listing_exist);
                        map.put("channelName", channel_name);
                        map.put("buhuo", buhuo);
                        map.put("availableDays", available_days);
                        map.put("replenishmentQuantity", replenishment_quantity);
                        map.put("suggestedReplenishmentTime", suggested_replenishment_time);
                        map.put("fulfillableQuantity", fulfillable_quantity);
                        map.put("inboundShippedQuantity", inbound_shipped_quantity);
                        map.put("inboundReceivingQuantity", inbound_receiving_quantity);
                        map.put("availableStock", available_stock);
                        map.put("reservedQuantity", reserved_quantity);
                        map.put("unsellableQuantity", unsellable_quantity);
                        map.put("inboundWorkingQuantity", inbound_working_quantity);
                        map.put("modifiedTime", modified_time);

                        System.out.println(map);
                    }
                }
*/
        } catch (Exception e) {
            e.printStackTrace();
        }
            return null;
    }

    @Override
    public boolean sync(Integer authorizationId, Date startTime, Date endTime) {
        SyncDto dto = new SyncDto();
        dto.setAuthorizationId(authorizationId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        dto.setBeanId("captain47ServiceImpl");
        TreeMap<String, String> params = new TreeMap<>();
        params.put("start_time", simpleDateFormat.format(startTime));
        params.put("end_time", simpleDateFormat.format(endTime));
        dto.setUrl(CaptainBIConstant.CaptainBIUrl.url_47.value());
        dto.setParams(params);
        jmsTemplate.convertAndSend(SyncListener.SYNC_LISTENER, dto);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertBatch(JSONArray jsonArray) {
        log.info("##########################################---47----批量保存------");
        List<Captain47Po> addList = new ArrayList<>();
        List<Captain47Po> updateList = new ArrayList<>();
        List<Captain47Po> captains = jsonArray.toJavaList(Captain47Po.class);

        for (Captain47Po captain : captains) {
            LambdaQueryWrapper<Captain47Po> la = new LambdaQueryWrapper<>();
            la.eq(Captain47Po::getSellerSku, captain.getSellerSku());
            if (count(la) == 0) {
                addList.add(captain);
            } else {
                LambdaUpdateWrapper laUpd = new LambdaUpdateWrapper();
                la.eq(Captain47Po::getSellerSku, captain.getSellerSku());
                update(captain,laUpd);
            }
        }
        if (CollectionUtil.isNotEmpty(addList)) {
            saveBatch(addList);
        }
        return true;
    }
}
